<template>
  <div class="create-cost-dialog">
    <el-dialog
      :visible.sync="visible"
      :append-to-body="false"
      :before-close="handleCancel"
      :class="{'global-mobile-dialog': showMobilePage}"
      class="dialog-size-auto small-common-dialog-size"
      width="450px">
      <div class="main">
        <Title title="项目名称" font-size="16px" style="margin-bottom:20px;"/>
        <el-form
          ref="projectForm"
          :rules="rules"
          :model="form"
          label-position="right"
          label-width="90px"
          class="lineHeight32">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :maxlength="50" class="width240" placeholder="请输入项目名称" size="small" />
          </el-form-item>
        </el-form>
        <div class="btn-container">
          <div v-loading="loading" class="confitm-btn">
            <el-button size="small" type="primary" @click="handleConfirm">确定</el-button>
          </div>
          <el-button type="info" class="cancel-btn-style" size="small" @click="handleCancel">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import { editReportDetail } from '@/api/publish';
import { mapState } from 'vuex';

export default {
  components: { Title },
  data() {
    return {
      visible: false,
      id: '',
      form: {
        projectName: ''
      },
      rules: {
        projectName: [{ required: true, trigger: 'input', message: '请输入项目名称' }]
      },
      loading: false
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  methods: {
    initDialog(obj) {
      this.form.projectName = obj.name;
      this.id = obj.id;
      this.visible = true;
    },
    handleConfirm() {
      this.loading = true;
      // 校验必填
      this.$refs['projectForm'].validate((valid) => {
        if (valid) {
          editReportDetail(this.id, this.form).then(res => {
            this.loading = false;
            this.$emit('updateData');
            this.handleCancel();
          }).catch(err => {
            this.loading = false;
            console.log(err);
          });
        } else {
          this.loading = false;
          return false;
        }
      });
    },
    handleCancel() {
      this.$refs['projectForm'].resetFields();
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.width240 {
  width: 240px;
}
.btn-container {
  display: flex;
  justify-content: center;
  .confitm-btn {
    margin-right: 10px;
  }
}
</style>
